package com.ibm.android.camera;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.Rect;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.view.SurfaceHolder;

public class ImageCamera implements ICamera {
	class ImageThread extends Thread {
		private boolean mRun = false;
		private SurfaceHolder mSurfaceHolder;

		public ImageThread(SurfaceHolder surfaceHolder) {
			mSurfaceHolder = surfaceHolder;
			mRun = false;
		}

		@Override
		public void run() {
			while (mRun) {
				Canvas c = null;
				try {
					c = mSurfaceHolder.lockCanvas(null);
					synchronized (mSurfaceHolder) {
						doDraw(c);
					}
				} finally {
					// do this in a finally so that if an exception is thrown
					// during the above, we don't leave the Surface in an
					// inconsistent state
					if (c != null) {
						mSurfaceHolder.unlockCanvasAndPost(c);
					}
				}
			}
		}

		private void doDraw(Canvas canvas) {
			if (mDrawable == null || canvas == null) {
				return;
			}

			Rect bounds = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
			Rect dest = new Rect(bounds);
			dest.bottom = mDrawable.getHeight() * bounds.right
					/ mDrawable.getWidth();
			dest.offset(0, (bounds.bottom - dest.bottom) / 2);
			Paint paint = new Paint();
			canvas.drawBitmap(mDrawable, null, dest, paint);

			if (mCallback != null) {
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				mDrawable.compress(CompressFormat.JPEG, 100, bos);
				mCallback.onPreviewFrame(bos.toByteArray(), null);
				if (mOneShot)
					mCallback = null;
			}
		}

		public void setRunning(boolean run) {
			mRun = run;
		}
	}

	private Parameters mParameters;
	private ImageThread mThread;
	private Bitmap mDrawable;
	private PreviewCallback mCallback;
	private boolean mOneShot;
	private Point mPreviewSize;

	public ImageCamera(Bitmap image) {
		mDrawable = image;
		mPreviewSize = new Point(image.getWidth(), image.getHeight());
		Camera c = Camera.open();
		mParameters = c.getParameters();
		mParameters.setPreviewFormat(PixelFormat.JPEG);
		mParameters.setPreviewSize(mPreviewSize.x, mPreviewSize.y);
		c.release();
	}

	@Override
	public void setPreviewDisplay(SurfaceHolder holder) throws IOException {
		mThread = new ImageThread(holder);
	}

	@Override
	public Parameters getParameters() {
		return mParameters;
	}

	@Override
	public void setParameters(Parameters p) {
		mParameters = p;
		// These are not variable for the HttpCamera. It's always the same.
		mParameters.setPreviewSize(mPreviewSize.x, mPreviewSize.y);
		mParameters.setPictureFormat(PixelFormat.JPEG);
	}

	@Override
	public void setOneShotPreviewCallback(PreviewCallback callback) {
		mCallback = callback;
		mOneShot = true;
	}

	@Override
	public void setPreviewCallback(PreviewCallback callback) {
		mCallback = callback;
		mOneShot = false;
	}

	@Override
	public void startPreview() {
		mThread.setRunning(true);
		mThread.start();
	}

	@Override
	public void stopPreview() {
		mThread.setRunning(false);
//		mThread.stop();
	}

	@Override
	public void open() {
		// TODO Auto-generated method stub

	}

	@Override
	public void release() {
		// TODO Auto-generated method stub

	}

}
